package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.order.service.PaymentService;
import com.atguigu.yygh.order.service.WeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName WeixinController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-10-22 18:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/order/weixin")
public class WeixinController {
    @Autowired
    private WeixinService weixinService;

    @Autowired
    private PaymentService paymentService;

    //下单 生成二维码
    @GetMapping("/createNative/{orderId}")
    public R createNative(@PathVariable Long orderId){
        Map map = weixinService.createNative(orderId);
        return R.ok().data(map);
    }

    @GetMapping("/queryPayStatus/{orderId}")
    public R queryPayStatus(@PathVariable Long orderId){
        //1. 调用查询接口
        Map<String,String> resultMap = weixinService.queryPayStatus(orderId);
        if (resultMap == null) {
            return R.error().message("支付出差");
        }
        //2. 支付成功
        if ("SUCCESS".equals(resultMap.get("trade_state"))){
            String out_trade_no = resultMap.get("out_trade_no");
            paymentService.paySuccess(out_trade_no,resultMap);
            return R.ok().message("支付成功");
        }
        return R.ok().message("支付中");
    }
}
